package leetcode.offer;

import org.junit.Test;

/**
 * @ClassName T29
 * @Description TODO
 * @Author Fanxc
 * @DATE 2021/4/10 21:40
 * @Version 1.0
 */
public class T29 {

    public int[] spiralOrder(int[][] matrix) {
        if(matrix.length == 0) return new int[0];
        int l = 0, r = matrix[0].length - 1, t = 0, b = matrix.length - 1, x = 0;
        int[] res = new int[(r + 1) * (b + 1)];
        while(true) {
            for(int i = l; i <= r; i++) System.out.println(matrix[t][i]);
            //res[x++] = matrix[t][i]; // left to right.
            if(++t > b) break;
            for(int i = t; i <= b; i++) System.out.println(matrix[i][r]);
                //res[x++] = matrix[i][r]; // top to bottom.
            if(l > --r) break;
            for(int i = r; i >= l; i--) System.out.println(matrix[b][i]);
                //res[x++] = matrix[b][i]; // right to left.
            if(t > --b) break;
            for(int i = b; i >= t; i--) System.out.println(matrix[i][l]);
                //res[x++] = matrix[i][l]; // bottom to top.
            if(++l > r) break;
        }
        return res;
    }

    int[][] a={{1,2,3},{4,5,6},{7,8,9}};
    @Test
    public void test(){
        spiralOrder(a);
    }
}
